package com.utils
{
	import com.utils.Print;
	
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.net.LocalConnection;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.*;

	/**
	 * 配置文件
	 * @author yangyang
	 *
	 */
	public class Config
	{

		public static const MAP_RECT : Rectangle = new Rectangle(4, -13, 800, 650); // 地图区域

		/**
		 * 帧频
		 */
		public static var frameRate : int = 24;
		/**
		 * 人物图层的容器
		 */
		public static var stage : Stage;

		public static var GRID_WIDTH : int;
		public static var GRID_HEIGHT : int;
		/**
		 * 屏幕大小
		 */
		public static var stageWidth : int;
		public static var stageHeight : int;

		public static function gc() : void
		{
			try
			{
				new LocalConnection().connect("gc");
				new LocalConnection().connect("gc");
			}
			catch(e : *)
			{
				Print.print("强制调用GC");
			}
		}


		/**
		 * 计算两点距离
		 * @param Ax	A点x坐标
		 * @param Ay	A点y坐标
		 * @param Bx	B点x坐标
		 * @param By	B点y坐标
		 */
		public static function getDistance(Ax : int, Ay : int, Bx : int, By : int) : int
		{
			return (Math.sqrt(((Ax - Bx) * (Ax - Bx)) + ((Ay - By) * (Ay - By))));
		}

		/**
		 * 计算亮点之间的角度
		 * @param Ax	A点x坐标
		 * @param Ay	A点y坐标
		 * @param Bx	B点x坐标
		 * @param By	B点y坐标
		 */
		public static function getRotation(Ax : int, Ay : int, Bx : int, By : int) : int
		{
			var tempXDistance : int = Bx - Ax;
			var tempYDistance : int = By - Ay;

			var rotation : int = Math.round(Math.atan2(tempYDistance, tempXDistance) * 57.33);
			rotation = (rotation + 360) % 360;

			return rotation;
		}


		/**
		 * 以角度为单位计算三角函数值
		 * @param angle
		 * @return
		 *
		 */
		public static function sinD(angle : Number) : Number
		{
			return Math.sin(getAngleByRotation(angle));
		}

		public static function cosD(angle : Number) : Number
		{
			return Math.cos(getAngleByRotation(angle));
		}

		public static function tanD(angle : Number) : Number
		{
			return Math.tan(getAngleByRotation(angle));
		}

		public static function getAngleByRotation(rotate : Number) : Number
		{
			return rotate * (Math.PI / 180);
		}

		public static function getRotationByAngle(angle : Number) : int
		{
			return Math.round(angle * 180 / Math.PI);
		}

		/**
		 * 只能克隆public属性的对象
		 * 构造函数也不能有参数
		 * @param obj
		 * @return
		 *
		 */
		public static function clone(obj : *) : *
		{
			if(obj == null || obj is int || obj is Number || obj is uint || obj is String || obj is Boolean)
			{
				return obj;
			}
			var data : XML = describeType(obj);
			var className : String = data.@name.toXMLString();
			var cloneClass : Class = getDefinitionByName(className) as Class;
			var cloneObj : * = new cloneClass();

			if(obj is Array || obj is Vector)
			{
				for each(var value : * in obj)
				{
					if(value is int || value is Number || value is uint || value is String || value is Boolean)
					{
						cloneObj.push(value);
					}
					else
					{
						cloneObj.push(clone(value));
					}
				}
			}

			if(data.@name == "Object")
			{
				readObject(cloneObj, obj);
			}
			else
			{
				for each(var variable : * in data.variable)
				{
					var key : String = String(variable.@name);
					cloneObj[key] = obj[key];
				}
			}
			return cloneObj;
		}

		public static function readObject(child : *, obj : Object) : void
		{
			var value : *;
			var key : String;
			var data : XML = describeType(child);

			if(data.@name == "Object")
			{
				for(key in obj)
				{
					child[key] = obj[key];
				}
				return;
			}

			for each(var variable : * in data.variable)
			{
				key = String(variable.@name);
				value = obj[key];

				if(value == null || value == undefined)
				{
					continue;
				}

				if(child[key] is int)
				{
					child[key] = int(value);
				}
				else if(child[key] is Number)
				{
					child[key] = Number(value);
				}
				else if(child[key] is Boolean)
				{
					child[key] = Boolean(int(value));
				}
				else if(child[key] is Array)
				{
					child[key] = clone(value);
				}
				else
				{
					child[key] = value;
				}
			}
		}

	}
}